<?php
class Blog_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
		$this->load->database();
	}
	
	/**
	 * new blog
	 */
	public function create_blog($blogname,$uid){
		$date_now = date('Y-m-d');
		$data = array(
				'bname' => $blogname,
				'creator' => $uid,
				'createdate' => $date_now
		);
		$this->db->insert('blog', $data);
		return $this->db->insert_id();
	}
	
	public function get_blog($bid){
		$this->db->where('bid',$bid);
		$query = $this->db->get('blog');
		if ($query->num_rows()>0) {
			$blog = $query->row_array();
			$blog['num_follow'] = $this->get_follow_num($bid);
			return $blog;
		} else {
			return null;
		}
	}
	
	public function get_follow_num($bid){
		$this->db->where('bid',$bid);
		return $this->db->count_all_results("follow");
	}
	
	public function add_editor($bid,$uid){
		if($this->check_manage($uid, $bid)||$this->check_own($uid, $bid)){
			return -1;
		} else {
			$date_now = date('Y-m-d');
			$data = array(
					'bid' => $bid,
					'uid' => $uid,
					'adddate' => $date_now,
			);
			$this->db->insert('manage', $data);
			return $this->db->insert_id();
		}
	} 
	
	public function get_editors($bid){
		$sql = "select uid,uname from user where uid in(
				select uid from manage where bid = ? 
			)";

		$query = $this->db->query($sql,array($bid));
		return $query->result_array();
	}
	
	public function remove_editor($bid,$editor){
		$this->db->delete('manage', array(
				'bid' => $bid,
				'uid' => $editor,
			));
	}
	
	public function get_creator($bid){
		$sql = "select uid,uname from user where uid in(
				select creator from blog where bid = ? 
			)";

		$query = $this->db->query($sql,array($bid));
		return $query->row_array();
	}
	
	public function blog_list($uid) {
		return array_merge(
				$this->get_own_blog_list($uid),
				$this->get_manage_blog_list($uid)
			);
		
	}
	
	public function default_blog($uid) {
		$ba = $this->blog_list($uid);
		if (count($ba))
			return $ba[0];
		else
			return null;
	}
	
	public function get_own_blog_list($uid,$detail = false){
		$this->db->select("bid,bname");
		$this->db->where('creator',$uid);
		$query = $this->db->get('blog');
		if ($detail) {
			return $this->attacth_detail($query->result_array());
		} else {
			return $query->result_array();
		}
	}
	
	public function get_manage_blog_list($uid,$detail = false){
		$sql = "select bid,bname from blog where bid in(
				select bid from manage where uid = ?
			)";
		$query = $this->db->query($sql,array($uid));
		if ($detail) {
			return $this->attacth_detail($query->result_array());
		} else {
			return $query->result_array();
		}
	}
	
	private function attacth_detail($blogs){
		foreach ($blogs as &$blog){
			$bid = $blog['bid'];
			$editors = $this->get_editors($bid);
			$blog['editors'] = $editors;
			$blog['creator'] = $this->get_creator($bid);
		}
		return $blogs;
	}
	
	public function check_manage($uid,$bid){
		$this->db->where('uid',$uid);
		$this->db->where('bid',$bid);
		$query = $this->db->get('manage');
		return $query->num_rows>0;
	}
	
	public function check_own($uid,$bid){
		$this->db->where('creator',$uid);
		$this->db->where('bid',$bid);
		$query = $this->db->get('blog');
		return $query->num_rows>0;
	}
	
	public function delete($bid){
		$this->db->delete('blog', array('bid' => $bid));
	}
	
}
?>